package com.ainge.easystudy.designpattern.singleton;

/**
 * 这种方式兼容了饿汉式的浪费内存已经饱汉式的synchronized性能问题
 * @author: Ainge
 * @Time: 2021/4/24 15:12
 */
public class LazyInnerSingleton {
    private LazyInnerSingleton(){
        if(LazyHolder.singleton != null){
            throw new RuntimeException("防止反射调用");
        }
    }
    public static final LazyInnerSingleton getInstance(){
        return LazyHolder.singleton;
    }
    // 内部类默认不加载
    private static final class LazyHolder{
        private static final LazyInnerSingleton singleton = new LazyInnerSingleton();
    }

    public static void main(String[] args) {
        LazyInnerSingleton.getInstance();
        LazyInnerSingleton.getInstance();
        LazyInnerSingleton.getInstance();
    }
}
